<template>
	<view class="address-box">
		<view class="address-list">
			<view class="item-list" v-for="(item, index) in addInfo" :key="index">
				<view class="info clearfix">
					<text class="left">{{item.name}}</text>
					<text class="right">{{item.phone | yinPhone}}</text>
				</view>
				<view class="address1">
					<text>{{item.address + item.juTi}}</text>
				</view>
				<view class="dom">
					<text class="use" @click="useBtn(index)">使用</text>
					<text class="edit" @click="editBtn(index)">编辑</text>
					<text class="del" @click="delBtn(index)">删除</text>
				</view>
			</view>
		</view> 
		<view class="addRess" @click="toAdd">
			<text>添加新地址</text>
		</view>
		<view style="position: fixed; top: 0; left: 0;width: 750rpx; height: 1400rpx;z-index: -1; background-color: #f5f5f9;"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addInfo: []
			};
		},
		filters:{
			yinPhone(tel) {
				tel = "" + tel;
				let tel1 = tel.substr(0,3) + "****" + tel.substr(7)
				return tel1
			}
		},
		methods: {
			initFunc() {
				uni.showLoading({
					title: '处理中...'
				})
				uniCloud.callFunction({
					name: "address",
					data: {
						type: 'get'
					}
				}).then(res => {
					uni.hideLoading()
					this.addInfo = res.result.data
				}).catch(err => {
					uni.hideLoading()
					console.log(err);
				})
			},
			toAdd() {
				uni.navigateTo({
					url: '/pages/addAddress/addAddress'
				})
			},
			useBtn(x) {
				let id = this.addInfo[x]._id
				uni.redirectTo({
					url: '/pages/commitOrder/commitOrder?id='+id
				})
			},
			editBtn(index) {
				let id = this.addInfo[index]._id
				uni.navigateTo({
					url: '/pages/editAddress/editAddress?id='+id
				})
			},
			delBtn(index) {
				let that = this
				let id = this.addInfo[index]._id
				uni.showLoading({
					title: '处理中...'
				})
				uniCloud.callFunction({
					name: "address",
					data: {
						type: 'del',
						data: {
							id: id
						}
					}
				}).then(res => {
					uni.hideLoading()
					if(res.success) {
						that.initFunc()
					}
				})
			}
			
		},
		onShow() {
			this.initFunc()
		}
	}
</script>

<style lang="scss" scoped>
	.address-list {
		width: $uni-width;
		.item-list {
			width: $uni-width;
			background-color: white;
			margin-bottom: 30rpx;
		}
		.info {
			padding-top: 10rpx;
			padding-left: 30rpx;
			box-sizing: border-box;
			width: 690rpx;
			height: 80rpx;
			line-height: 80rpx;
			.name1 {
				text-align: left;
			}
			.phone1 {
				text-align: right;
			}
		}
		.address1 {
			padding-top: 10rpx;
			padding-left: 30rpx;
			box-sizing: border-box;
			margin-bottom: 10rpx;
		}
		.dom {
			padding-top: 10rpx;
			padding-left: 30rpx;
			padding-right: 60rpx;
			box-sizing: border-box;
			border-top: 2rpx solid #ddd;
			height: 80rpx;
			line-height: 60rpx;
			text-align: right;
			.edit {
				margin: 0 40rpx;
				color: #2babd6;
			}
			.del {
				color: red;
			}
			.use {
				color: blue;
			}
		}
	}
	.addRess {
		position: fixed;
		bottom: 0;
		width: $uni-width;
		background-color: #01c2c3;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		color: white;
		font-size: 40rpx;
		font-weight: 600;
	}
</style>
